//
//  HRVoiceCardCell.m
//  Hear
//
//  Created by mac on 2017/3/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRVoiceCardCell.h"
#import "HRMainRootModel.h"
#import "NSMutableAttributedString+GDAttString.h"
@interface HRVoiceCardCell ()
{
    BOOL animating;
}
@property (nonatomic, strong)UIImageView *coverImageView;
@property (nonatomic, strong)UIImageView *backImageView;
@property (nonatomic, strong)UIButton *iconImageView;
@property (nonatomic, strong)UILabel *nickNameLabel;

@end
@implementation HRVoiceCardCell
- (void)prepareForReuse {
    [super prepareForReuse];
    self.backImageView.image = nil;
}
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

-(void)setMainModel:(HRMainRootModel *)mainModel
{
    _mainModel = mainModel;
    if ([mainModel.UserLocalId isEqualToString:@"admin"]){
        [self.iconImageView setImage:[UIImage imageNamed:@"小助手"] forState:0];
    }else{
        [self.iconImageView setImage:[UIImage imageNamed:mainModel.UserPictureName] forState:0];
        self.iconImageView.backgroundColor = [UIColor colorWithHexString:mainModel.UserPictureBackgroundColor];
    }
    self.nickNameLabel.attributedText = [NSMutableAttributedString attStringFromTextForChatting:mainModel.NickName textFont:CustomUIFont(14)];
    self.nickNameLabel.textAlignment = 1;

    if(!mainModel.BackGround.length){
        mainModel.BackGround = [NSString stringWithFormat:@"soundcover%d",[self getRandomNumber:1 to:8]];
    }
    self.coverImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:mainModel.BackGround ofType:@"jpg"]];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        [self setupViews];
        
    }
    return self;
}

- (void)setupViews
{
    if (self.backImageView) {
        return;
    }
    
    UIImageView *backImageView = [UIImageView new];
    [self addSubview:(self.backImageView = backImageView)];
    backImageView.userInteractionEnabled = YES;
    
    UIImageView *coverImageView = [UIImageView new];
    [self addSubview:(self.coverImageView = coverImageView)];
    coverImageView.userInteractionEnabled = YES;
    coverImageView.clipsToBounds = YES;
    
    UIButton *iconImageView = [UIButton new];
    [self addSubview:(self.iconImageView = iconImageView)];
    iconImageView.imageView.contentMode = UIViewContentModeScaleAspectFit;
    iconImageView.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    iconImageView.layer.borderColor = [[UIColor whiteColor]colorWithAlphaComponent:0.5].CGColor;
    iconImageView.layer.borderWidth = 5;
    [iconImageView addTarget:self action:@selector(lookInfoClick) forControlEvents:UIControlEventTouchUpInside];
    WeakSelf
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(30);
        make.right.bottom.equalTo(weakSelf.contentView).offset(-30);
    }];
    
    self.nickNameLabel = [UILabel new];
    self.nickNameLabel.textColor = [UIColor whiteColor];
    self.nickNameLabel.font = CustomUIFont(14);
    [self addSubview:self.nickNameLabel];
    self.nickNameLabel.numberOfLines = 0;
    self.nickNameLabel.textAlignment = 1;
    iconImageView.layer.cornerRadius = 35;

    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backImageView);
        make.top.equalTo(backImageView.mas_top).offset(80);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    
    CGFloat coverWidth = 130;
    if (IS_IPHONE_5)
    {
        coverWidth = 205;
    }else if (IS_IPHONE_6){
        coverWidth = 230;
    }else if (IS_IPHONE_6P){
        coverWidth = 255;
    }
    self.coverImageView.layer.cornerRadius = coverWidth/2;

    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(backImageView);
        make.size.mas_equalTo(CGSizeMake(coverWidth, coverWidth));
    }];
    
    [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.iconImageView.mas_bottom).offset(20);
        make.centerX.equalTo(backImageView);
        make.size.mas_equalTo(CGSizeMake(coverWidth-60, 50));
    }];
    

    
}

- (void)lookInfoClick{
    if (self.lookInfoClickBlock){
        self.lookInfoClickBlock();
    }
}

- (void)configureCellWithPostURL:(NSString *)coverUrl
{
    self.backImageView.image = [UIImage imageNamed:@"唱片"];
    if ([self.backImageView.image isEqual:[UIImage imageNamed:@"唱片"]])
    {
        CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        
        //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
        
        animation.fromValue = [NSNumber numberWithFloat:0.f];
        
        animation.toValue =  [NSNumber numberWithFloat: M_PI/2.0];
        
        animation.duration  = 10.0f;
        animation.cumulative = YES;
        
        animation.removedOnCompletion = NO;
        animation.repeatCount = HUGE_VALF;
        
        [self.backImageView.layer addAnimation:animation forKey:@"animation"];
    }

}




- (void)startTransformAnimation
{

    CFTimeInterval pausedTime = [self.backImageView.layer
                                 timeOffset];
    self.backImageView.layer.speed = 1.0;
    self.backImageView.layer.timeOffset = 0.0;
    
    self.backImageView.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [self.backImageView.layer
                                     convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.backImageView.layer.beginTime = timeSincePause;
    

}
- (void)stopTransformAnimation
{
    CFTimeInterval pausedTime = [self.backImageView.layer
                                 convertTime:CACurrentMediaTime() fromLayer:nil];
    self.backImageView.layer.speed = 0.0;
    
    self.backImageView.layer.timeOffset = pausedTime;
    

}
@end
